package com.app.ui.activity.pay;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.app.net.manager.pay.PayWXManager;
import com.app.net.manager.pay.PayZFBManager;
import com.app.net.res.pay.PayWXRes;
import com.app.ui.activity.base.BaseActivity;
import com.app.utiles.other.Constant;
import com.app.utiles.other.NumberUtile;
import com.app.utiles.other.ToastUtile;
import com.pay.zfb.PayAlipayManager;
import com.t.t.R;
import com.tencent.mm.opensdk.modelpay.PayReq;

import java.text.SimpleDateFormat;
import java.util.Date;

//支付宝支付和微信支付
public class PayActivity extends BaseActivity implements View.OnClickListener {


    private PayWXManager payWXManager;
    private PayZFBManager payCertificateManager;
    private PayAlipayManager alipayManager;
    private ImageView zfbIv, wxIv;
    private EditText payEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);

        findViewById(R.id.pay_tv).setOnClickListener(this);
        findViewById(R.id.pay_zfb_rl).setOnClickListener(this);
        findViewById(R.id.pay_wx_rl).setOnClickListener(this);
        payEt = (EditText) findViewById(R.id.pay_et);
        zfbIv = (ImageView) findViewById(R.id.pay_zfb_iv);
        wxIv = (ImageView) findViewById(R.id.pay_wx_iv);
    }

    @Override
    public void onClick(View v) {
        onClick(v.getId());
    }

    private boolean isZfb = true;

    private void onClick(int id) {
        switch (id) {
            case R.id.pay_zfb_rl:
                //支付宝支付
                isZfb = true;
                zfbIv.setImageResource(R.mipmap.pay_check_true);
                wxIv.setImageResource(R.mipmap.pay_check_false);
                break;
            case R.id.pay_wx_rl:
                //微信支付
                isZfb = false;
                zfbIv.setImageResource(R.mipmap.pay_check_false);
                wxIv.setImageResource(R.mipmap.pay_check_true);
                break;
            case R.id.pay_tv:
                //确认支付
                if (isZfb) {
                    zfbPay();
                    break;
                }
                wxPay();
                break;
        }

    }

    private void zfbPay() {
        if (payCertificateManager == null) {
            payCertificateManager = new PayZFBManager(this);
        }
        String p = payEt.getText().toString();
        //分
        int price = (int) NumberUtile.geStringToFloat(p) * 100;
        if (price == 0) {
            ToastUtile.showToast("请输入支付金额");
            return;
        }
        String orderId = getTime();
        payCertificateManager.setData(orderId, price, "测试（标题）", "测试（描述）");
        payCertificateManager.doRequest();
        dialogShow();
    }

    //微信支付
    private void wxPay() {
        com.pay.wx.PayWXManager.getInstance().initWX(this, Constant.WX_APP_ID);
        com.pay.wx.PayWXManager.getInstance().setDbug(true);
        if (payWXManager == null) {
            payWXManager = new PayWXManager(this);
        }
        String p = payEt.getText().toString();
        //分
        int price = (int) NumberUtile.geStringToFloat(p) * 100;
        if (price == 0) {
            ToastUtile.showToast("请输入支付金额");
            return;
        }
        String orderId = getTime();
        payWXManager.setData(orderId, price, "微信支付（标题）", "微信支付（描述）");
        payWXManager.doRequest();
        dialogShow();
    }

    private String getTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = simpleDateFormat.format(new Date());
        return time;
    }

    @Override
    public void onBackPressed() {
        if (isPay) {
            ToastUtile.showToast("正在支付,请稍等...");
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        dialogDismiss();
    }

    @Override
    public void OnBack(int what, Object obj, String msg, String other) {
        switch (what) {
            case PayZFBManager.PAY_WHAT_SUCCEED:
                //获取支付凭证成功
                String zfbCertificate = (String) obj;
                //调用支付宝支付
                if (alipayManager == null) {
                    alipayManager = new PayAlipayManager(this);
                    alipayManager.setOnPayListener(new PayOnListener());
                }
                isPay = true;
                alipayManager.pay(zfbCertificate);
                break;
            case PayZFBManager.PAY_WHAT_FAILED:
                dialogDismiss();
                break;
            case PayWXManager.WHAT_DEAL_SUCCEED:
                //获取微信支付信息成功
                PayWXRes res = (PayWXRes) obj;
                //
                PayReq req = new PayReq();
                //微信开放平台审核通过的应用APPID
                req.appId = Constant.WX_APP_ID;  // 测试用appId
                //微信支付分配的商户号
                req.partnerId = Constant.WX_PARTNE_ID;
                //微信返回的支付交易会话ID
                req.prepayId = res.pay_str;
                //随机字符串，不长于32位。推荐随机数生成算法
                req.nonceStr = res.nonce_str;
                //时间戳 10位(s)
                req.timeStamp = res.timestamp;
                req.packageValue = "Sign=WXPay";
                req.sign = res.sign;
                Log.e("微信支付签名", req.sign);
                com.pay.wx.PayWXManager.getInstance().payWX(req);
                // com.pay.wx.PayWXManager.getInstance().payWX(Constant.WX_PARTNE_ID, Constant.WX_PARTNE_KEY,
                //         res.pay_str, res.timestamp);
                break;
            case PayWXManager.WHAT_DEAL_FAILED:
                //获取支付信息失败
                dialogDismiss();
                break;
        }
        super.OnBack(what, obj, msg, other);
    }

    private boolean isPay;


    //支付宝支付回调
    class PayOnListener implements PayAlipayManager.PayListener {

        @Override
        public void onPaySucceed(double money) {
            isPay = false;
            dialogDismiss();
        }

        @Override
        public void onPayAffirm(double money) {
        }

        @Override
        public void onPayFailed(double money, String code, String msg) {
            isPay = false;
            dialogDismiss();
        }

    }
}
